package cn.sumpu.app.coolsk.dao.impl;

import java.util.*;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.*;
import cn.sumpu.app.coolsk.dao.*;
import cn.sumpu.app.coolsk.domain.*;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class InfoDaoImpl extends SqlSessionDaoSupport implements InfoDao {
	private final static Logger logger = LoggerFactory
			.getLogger(InfoDaoImpl.class);

	//添加信息
	@Override
	public void addInfo(Info info) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.InfoMapper.addInfo", info);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("addInfo resource failure.", e);
			}
			throw new AppBusinessException("addInfo resource failure.", e);
		}
	}
	//查询信息
	@SuppressWarnings("unchecked")
	@Override
	public List<Info> getInfo(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.InfoMapper.getInfo", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getInfo resource failure.", e);
			}
			throw new AppBusinessException("getInfo resource failure.", e);
		}
	}
	//查询信息总条数
	@Override
	public Integer getInfoCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.InfoMapper.getInfoCount", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getInfoCount resource failure.", e);
			}
			throw new AppBusinessException("getInfoCount resource failure.", e);
		}
	}
	//删除信息
	@Override
	public void deleteInfo(String id) throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.coolsk.persistence.InfoMapper.deleteInfo", id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteInfo resource failure.", e);
			}
			throw new AppBusinessException("deleteInfo resource failure.", e);
		}
	}
	//更新信息
	@Override
	public void updateInfo(Info info) throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.InfoMapper.updateInfo", info);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteInfo resource failure.", e);
			}
			throw new AppBusinessException("deleteInfo resource failure.", e);
		}
	}
	//通过ID取得某条信息
	@Override
	public Info getInfoById(String id) throws AppBusinessException {
		try {
			return (Info) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.InfoMapper.getInfoById", id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getInfoById resource failure.", e);
			}
			throw new AppBusinessException("getInfoById resource failure.", e);
		}
	}
	//更新浏览次数
	@Override
	public void updateInfoViews(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.InfoMapper.updateInfoViews", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateInfoViews resource failure.", e);
			}
			throw new AppBusinessException("updateInfoViews resource failure.", e);
		}
	}
	//更新点评数
	@Override
	public void updateInfoComments(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.InfoMapper.updateInfoComments", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateInfoComments resource failure.", e);
			}
			throw new AppBusinessException("updateInfoComments resource failure.", e);
		}
	}
	
}
